package livesun.justiceeternal.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.zzhoujay.richtext.RichText;
import com.zzhoujay.richtext.callback.OnImageClickListener;

import java.util.ArrayList;
import java.util.List;

import br.tiagohm.markdownview.MarkdownView;
import br.tiagohm.markdownview.css.styles.Github;
import livesun.justiceeternal.R;
import livesun.justiceeternal.activity.PhotoActivity;
import livesun.justiceeternal.base.BaseActivity;
import livesun.justiceeternal.db.love.LoveBean;
import livesun.justiceeternal.utils.ConstantUtil;
import livesun.justiceeternal.utils.MatcherUtils;
import livesun.justiceeternal.utils.PreferenceUtil;

public class LoveActivity extends BaseActivity {

    private String headBody;

    @Override
    public int getResourcesId() {

        return R.layout.activity_love;
    }

    @Override
    protected CharSequence getMiddleTitle() {
        return "收藏";
    }

    @Override
    public void initView() {
        RichText.initCacheDir(getCacheDir());
        LoveBean loveBean = (LoveBean) getIntent().getSerializableExtra("body");
        headBody = loveBean.getBody();
        if( !PreferenceUtil.getBoolean(ConstantUtil.SWITCH_MODE_KEY, false)){
            headBody = MatcherUtils.deletComment(headBody);
            headBody =MatcherUtils.getInfor(headBody);
            TextView textView =  findView(R.id.content_main);
            textView.setVisibility(View.VISIBLE);
            RichText.fromMarkdown(headBody).singleLoad(true).bind(this)
                    .autoPlay(true)
                    .imageClick(new OnImageClickListener() {
                        @Override
                        public void imageClicked(List<String> imageUrls, int position) {
                            Intent intent = new Intent(LoveActivity.this, PhotoActivity.class);
                            intent.putStringArrayListExtra("IMGES", (ArrayList<String>) imageUrls);
                            intent.putExtra("position",position);
                            startActivity(intent);

                        }
                    }).into(textView);
        }else{
            MarkdownView mMarkdownView =findView(R.id.markdown_view);
            mMarkdownView.setVisibility(View.VISIBLE);
            mMarkdownView.addStyleSheet(new Github());
            mMarkdownView.loadMarkdown(headBody);
        }
    }

    @Override
    public void initListener() {

    }

    @Override
    public void onClick(View v, int id) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RichText.clear(this);
    }
}
